package pl.textureStudio.view.textureList
{
	import flash.events.MouseEvent;
	import pl.textureStudio.model.StudioModel;
	import com.bit101.components.PushButton;
	import pl.textureStudio.data.IStorage;

	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;

	/**
	 * @author michalr
	 */
	public class TextureList extends Sprite implements ITextureList
	{
		private var container:Sprite;
		private var panel:Shape;
		private var storage:IStorage;
		private var margin:uint = 5;
		private var itemsSpace:Rectangle = new Rectangle();
		// ...
		private static const FRAME_SIZE:uint = 30;

		public function TextureList()
		{
			container = new Sprite();
			addChild( container );
			init();
			itemsSpace.x = margin;
			itemsSpace.y = FRAME_SIZE + margin ;
			container.x = itemsSpace.x;
			container.y = itemsSpace.y;
			addEventListener( Event.ADDED_TO_STAGE, onATS );
		}

		private function onATS( event:Event ):void
		{
			removeEventListener( Event.ADDED_TO_STAGE, onATS );
			stage.addEventListener( Event.RESIZE, onSR );
		}

		private function onSR( event:Event ):void
		{
			panel.width = stage.stageWidth;
			panel.height = FRAME_SIZE;
			realignItems();
		}

		private function init():void
		{
			panel = new Shape();
			addChild( panel );
			var g:Graphics = panel.graphics;
			g = panel.graphics;
			g.beginFill( 0x333333, 1 );
			g.drawRect( 0, 0, 1, 1 );
			g.endFill();
			// ...
			// panel gui
			new PushButton(this,5,5,'Add new texture',handleAddNewTexture);
		}

		private function handleAddNewTexture(e:MouseEvent):void
		{
			StudioModel.getInstance().addNewTexture();
		}		

		private function fill():void
		{
			var i:uint = 0;
			var n:uint = storage.dataLength;
			var item:TextureItem;
			while (i < n)
			{
				item = TextureItem.factory( container, storage.getVOAtIndex( 0 ) );
				i++;
			}
			realignItems();
			trace( 'cnum', container.numChildren );
		}

		private function realignItems():void
		{
			var sw:uint = stage.stageWidth;
			var sh:uint = stage.stageHeight;
			itemsSpace.width = sw - itemsSpace.x - margin;
			itemsSpace.height = sh - itemsSpace.y - margin;
			container.graphics.clear();
			container.graphics.beginFill( 0, .3 );
			container.graphics.drawRect( 0, 0, itemsSpace.width, itemsSpace.height );
			container.graphics.endFill();
			var minimalGap:uint = 10;
			var hFitCount:uint = Math.floor( itemsSpace.width / (TextureItem.SIZE + minimalGap) );
			// hFitCount = hFitCount > container.numChildren ? container.numChildren : hFitCount;
			var hFitSize:uint = hFitCount * TextureItem.SIZE;
			var sizeLeft:uint = itemsSpace.width - hFitSize;
			var gap:uint = Math.floor( sizeLeft / (hFitCount + 1) );
			trace( 'size left', sizeLeft, 'gap', gap, 'hfit count ', hFitCount, 'hfitSize', hFitSize );

			var i:uint = 0;
			var n:uint = container.numChildren;
			var item:DisplayObject;

			var ox:uint = 0;
			var oy:uint = 0;
			gap = hFitCount >= n ? minimalGap : gap;
			while (i < n)
			{
				item = container.getChildAt( i );
				item.x = ox * (TextureItem.SIZE + gap);
				item.y = oy * (TextureItem.SIZE + gap);
				if (ox == hFitCount - 1)
				{
					ox = 0;
					oy++;
				}
				else ox++;
				i++;
			}

			// var hCount : uint = (sw/)
		}

		// TODO : UPDATE klockow
		public static function factory( parent:DisplayObjectContainer, storage:IStorage ):ITextureList
		{
			var o:TextureList = new TextureList();
			o.storage = storage;
			o.hide();
			parent.addChild( o );
			return o as ITextureList;
		}

		public function show():void
		{
			visible = true;
			if (container.numChildren == 0) fill();
		}

		public function hide():void
		{
			visible = false;
		}
	}
}
